package com.ll.springbucks;

import com.ll.springbucks.repository.CoffeeRepository;
import com.ll.springbucks.model.Coffee;
import com.ll.springbucks.model.CoffeeOrder;
import com.ll.springbucks.model.OrderState;
import com.ll.springbucks.service.CoffeeOrderService;
import com.ll.springbucks.service.CoffeeService;
import io.lettuce.core.ReadFrom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Optional;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@EnableJpaRepositories
public class SpringBucksApplication   {
//	@Autowired
//	private CoffeeRepository coffeeRepository;
//	@Autowired
//	private CoffeeService coffeeService;
//	@Autowired
//	private CoffeeOrderService orderService;

	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

//	@Override
//	public void run(ApplicationArguments args) throws Exception {
//
//	}

//	@Override
//	public void run(ApplicationArguments args) throws Exception {
//		log.info("All Coffee: {}", coffeeRepository.findAll());
//
//		Optional<Coffee> latte = coffeeService.findOneCoffee("Latte");
//		if (latte.isPresent()) {
//			CoffeeOrder order = orderService.createOrder("Li Lei", latte.get());
//			log.info("Update INIT to PAID: {}", orderService.updateState(order, OrderState.PAID));
//			log.info("Update PAID to INIT: {}", orderService.updateState(order, OrderState.INIT));
//		}
////	}
//
//	@Bean
//	public RedisTemplate<String, Coffee> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//		RedisTemplate<String, Coffee> template = new RedisTemplate<>();
//		template.setConnectionFactory(redisConnectionFactory);
//		return template;
//	}
//	@Bean
//	public LettuceClientConfigurationBuilderCustomizer customizer() {
//		return builder -> builder.readFrom(ReadFrom.MASTER_PREFERRED);
//	}
//
//
//	@Override
//	public void run(ApplicationArguments args) throws Exception {
//		Optional<Coffee> c = coffeeService.findOneCoffee("mocha");
//		log.info("Coffee {}", c);
//
//		for (int i = 0; i < 5; i++) {
//			c = coffeeService.findOneCoffee("mocha");
//		}
//
//		log.info("Value from Redis: {}", c);
//	}
}

